package com.gvsoft.analyse;

import com.gvsoft.Client;
import com.gvsoft.analyse.inf.IServerPacket;
import com.gvsoft.analyse.packet.ServerPacket;


import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.charset.CharacterCodingException;
import java.nio.charset.Charset;
import java.nio.charset.CharsetDecoder;
import java.nio.charset.CharsetEncoder;
import java.util.StringTokenizer;

/**
 * Created with IntelliJ IDEA.
 * User: zhaoqiubo
 * Date: 15/7/27
 * Time: 上午10:38
 * 报文分析工具类
 */
public class AnalyseTools {

    private static final String UTF_8 = "UTF-8";
    private static CharsetEncoder encoder = Charset.forName(UTF_8).newEncoder();
    private static CharsetDecoder decoder = Charset.forName(UTF_8).newDecoder();

    /**
     * 拆分字符串生成handle对象
     * @param strA
     * @return
     */
    public static IServerPacket analyseServerPacket(String strA) {

        int posA = strA.indexOf(Client.HBREGEX);
        if (posA > 0) {
            ServerPacket sp = new ServerPacket();
            String strB = strA.substring(posA + 1);
            int posB = strB.indexOf(Client.HBREGEX);
            if (posB > 0) {
                String strC = strB.substring(posB+1);
                int posC = strC.indexOf(Client.HBREGEX);
                if(posC >0) {
                    sp.generatePacket(strA.substring(0, posA), strB.substring(0, posB), strC.substring(0,posC),strC.substring(posC + 1));
                    return sp;
                }
                sp.generatePacket(strA.substring(0, posA), strB.substring(0, posB), strB.substring(posB + 1));
                return sp;
            }else{
                sp.generatePacket(strA.substring(0,posA),strA.substring(posA+1));
                return sp;
            }
        } else {
            return null;
        }

    }

    public static ByteBuffer encode(CharBuffer in) throws CharacterCodingException {
        return encoder.encode(in);
    }

    public static CharBuffer decode(ByteBuffer in) throws CharacterCodingException {
        return decoder.decode(in);
    }

    public static boolean isContain(String[] strArray, String str) {

        for (int i = 0; i < strArray.length; i++) {
            if (strArray[i].equals(str)) {
                return true;
            }

        }
        return false;
    }

    /*
    根据分隔符，将字符串分割为数组
     */
    public static String[] str2ArrayByChar(String string, String divisionChar) {
        int i = 0;
        StringTokenizer tokenizer = new StringTokenizer(string, divisionChar);

        String[] str = new String[tokenizer.countTokens()];

        while (tokenizer.hasMoreTokens()) {
            str[i] = new String();
            str[i] = tokenizer.nextToken().trim();
            i++;
        }

        return str;
    }

    public static void main(String arg[]) throws Exception {

        String str = "L|42342341|{\"clientId\":\"0e73b8bf-a00b-f98c-f49c-4e1dc1c162d9\"}";
        AnalyseTools.analyseServerPacket(str);
//        String str = "R:com.gvsoft.analyse.LoginHandle|E:com.gvsoft.analyse.ErrorHandle";
//        String[] strArray = AnalyseTools.str2ArrayByChar(str, "|");
        System.out.println("S");


    }
}
